#!/bin/bash
# Script to run the Streamlit application for product attribute matching

echo "===== 启动智能产品属性匹配系统 ====="
echo "正在检查环境..."

# Check if Python is installed
if ! command -v python3 &> /dev/null; then
    echo "错误: 未找到Python3，请安装Python3后重试"
    exit 1
fi

# Check if pip is installed
if ! command -v pip3 &> /dev/null; then
    echo "错误: 未找到pip3，请安装pip3后重试"
    exit 1
fi

# Check if requirements are installed
echo "正在检查依赖..."
pip3 install -r requirements.txt

# 设置环境变量
export PYTHONPATH="$PYTHONPATH:$(pwd)"

# 设置Streamlit环境变量，优化性能和启动
export STREAMLIT_SERVER_MAX_UPLOAD_SIZE=200
export STREAMLIT_SERVER_RUN_ON_SAVE=false
export STREAMLIT_BROWSER_GATHER_USAGE_STATS=false

# 使用一个内存优化的函数预热模型
echo "正在预加载模型，这可能需要几分钟时间..."
python3 -c "
import os
os.environ['PYTHONUNBUFFERED'] = '1'
print('正在初始化模型...')
from attribute_matching import AttributeMatcher
print('创建匹配器实例...')
matcher = AttributeMatcher()
# 执行一次匹配操作以确保模型完全加载
print('预热模型...')
matcher.find_best_attribute_matches({'test': ['value']}, {'test': 'value'})
print('模型已预加载并预热完成')
"

# 启动应用
echo "正在启动Streamlit应用..."
streamlit run streamlit_app.py --server.port 9000